﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class MenuItemCategoryDAO : MySQLDAO, IMenuItemCategoryDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<MenuItemCategoryDTO> cmdExec;

        #endregion

        #region Contructor

        public MenuItemCategoryDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<MenuItemCategoryDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO menu_item_category_tbl (menu_item_id, menu_category_id) " +
            "VALUES (@menu_item_id, @menu_category_id)";
        public void Insert(MenuItemCategoryDTO dto)
        {
            cmdExec.PerformNonQuery(insertSQL, loadParametersFromDTO(dto));           
        }

        public MenuItemCategoryDTO SelectByID(long id)
        {
            throw new NotImplementedException();
        }

        private const string selectByIDSQL =
            "SELECT * FROM menu_item_category_tbl WHERE menu_item_id = @menu_item_id";
        /// <summary>
        /// Select a MenuItemCategory object by MenuItem ID, *NOT* the MenuCategory ID. The
        /// MenuItem ID is the more prominent id. There will be a separate call to do the
        /// reverse lookup.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<MenuItemCategoryDTO> SelectByMenuID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = id;

            MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate loadMenuItemCategoryDTO = 
                new MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate(loadDTOFromReader);
            return cmdExec.PerformSelectQuery(selectByIDSQL, parameters, loadMenuItemCategoryDTO);                       
        }

        private const string selectByCategoryIDSQL =
            "SELECT * FROM menu_item_category_tbl WHERE menu_category_id = @menu_category_id";
        /// <summary>
        /// Select a MenuItemCategory object by MenuCategoryID. If you want to lookup by MenuItemID 
        /// use the SelectByMenuID method.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<MenuItemCategoryDTO> SelectByCategoryID(long categoryID)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_category_id"] = categoryID;

            MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate loadMenuItemCategoryDTO =
                new MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate(loadDTOFromReader);
            return cmdExec.PerformSelectQuery(selectByCategoryIDSQL, parameters, loadMenuItemCategoryDTO);
        }

        const string selectAllSQL = 
            "SELECT * FROM menu_item_category_tbl";
        public IList<MenuItemCategoryDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate loadMenuItemCategoryDTO = 
                new MySQLDAOCmdExecuter<MenuItemCategoryDTO>.LoadDTODelegate(loadDTOFromReader);
            return cmdExec.PerformSelectQuery(selectAllSQL, null, loadMenuItemCategoryDTO);       
        }
      
        public void Update(MenuItemCategoryDTO dto)
        {
            throw new NotImplementedException();
        }

        const string deleteByIDSQL = 
            "DELETE FROM menu_item_category_tbl WHERE menu_item_id = @menu_item_id";
        /// <summary>
        /// Delete a MenuItemCategory object by MenuItem ID, *NOT* the MenuCategory ID. The
        /// MenuItem ID is the more prominent id. There will be a separate call to do the
        /// reverse lookup.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(MenuItemCategoryDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = dto.MenuItemID;
            parameters["@menu_category_id"] = dto.MenuCategoryID;
            return parameters;
        }

        private MenuItemCategoryDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            MenuItemCategoryDTO dto = new MenuItemCategoryDTO();
            dto.MenuItemID = dataReader.GetInt64("menu_item_id");
            dto.MenuCategoryID = dataReader.GetInt64("menu_category_id");
            return dto;
        }

        #endregion
    }
}
